/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPSocketCommunicator.
//
// UDPSocketCommunicator uses to send/receive UDP messages.
//

#ifndef __UDP__SOCKET__COMMUNICATOR__H
#define __UDP__SOCKET__COMMUNICATOR__H

#include <netinet/in.h>

#include "udp_socket.h"
#include "message_packet.h"

namespace amz {

class UDPSocketCommunicator {
 public:
  UDPSocketCommunicator(CompressType compress, EncryptType encrypt,
         const sockaddr_in &address, int len, UDPSocket *udp_socket, int flag = 0);
  virtual ~UDPSocketCommunicator();

  // Set destination of address.
  // return 0 sucess.
  // return -1 is an error value.
  int SetDestinationAddress(const sockaddr_in *addr, int len);

  // Check send.
  inline virtual bool CanSend(int size) { return true; }

  // Send.
  // Packet pattern: send to socket.
  // Stream pattern: send to buffer.
  virtual int Send(const char *message, int size) = 0;

  // Receive.
  // Packet pattern: Receive from socket.
  // Stream pattern: Receive from buffer.
  virtual int Receive(char *message, int max_size) = 0;

  // Use for UDP socket.
  virtual int Send() { return 0; }
  
  // After receiving any data, dispatch to the buffer(according to address).
  virtual int PushReceive(char *stream, int size) { return 0; }


  // --------------------------------------------------------------------------
  // For the credible communication.
  virtual void CredibleTimeoutResend() {}
  virtual void CredibleOrderReveive() {}
  // --------------------------------------------------------------------------

 protected:
  // IP address.
  struct sockaddr_in address_in4_;
  // udp socket pointer.
  UDPSocket *udp_socket_;
  // flag of send/receive.
  int flag_;
  // Packet tool.
  MessagePacket packet_;
};

}  // namespace amz

#endif  // __UDP__SOCKET__COMMUNICATOR__H
